package com.itheima.controller;

import com.itheima.domain.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Controller
@ResponseBody
public class UserController {
    @RequestMapping("ajaxcontroller")
    public String ajaxTest1(@RequestBody String message) {
        int i = 1 / 0;
        System.out.println(message);
        return message;
    }

    @RequestMapping("ajaxcontroller02")
    public User ajaxTest2(@RequestBody User user) {
        System.out.println(user);
        return user;
    }

    @RequestMapping("ajaxcontroller03")
    public List<User> ajaxTest3(@RequestBody List<User> users) {
        System.out.println(users);
        return users;
    }

    @RequestMapping("ajaxcontroller04")
    public String ajaxTest4(@RequestBody String s) {
        System.out.println(s);
        return s;
    }

    @RequestMapping("ajaxcontroller05")
    public User ajaxTest5() {
        User user = new User();
        user.setUsername("zhangsan");
        user.setAge(25);
        return user;
    }

    @RequestMapping("ajaxcontroller06")
    public List<User> ajaxTest6() {
        ArrayList<User> users = new ArrayList<>();
        User user = new User();
        user.setUsername("zhangsan");
        user.setAge(25);
        users.add(user);
        return users;
    }

    @RequestMapping("fileUpLoad")
    public String fileUpLoad(MultipartFile[] file) throws IOException {


        for (MultipartFile multipartFile : file) {
            if (multipartFile != null) {
                String s = UUID.randomUUID().toString();
                String originalFilename = multipartFile.getOriginalFilename();
                String s1 = StringUtils.substringAfterLast(originalFilename, ".");
                String s2 = s + "." + s1;
                multipartFile.transferTo(new File(s2));
            }
        }

        return "success";

    }

    @GetMapping("/restful01/{id}")
    public User restful01(@PathVariable int id) {
        System.out.println("get");
        User user = new User();
        user.setUsername("zhangsan");
        user.setAge(25);
        return user;
    }

    @PostMapping("restful02")
    public User restful02() {
        System.out.println("post");
        User user = new User();
        user.setUsername("zhangsan");
        user.setAge(25);
        return user;
    }

    @DeleteMapping("restful03/{id}")
    public User restful03(@PathVariable int id) {
        System.out.println("delete");
        User user = new User();
        user.setUsername("zhangsan");
        user.setAge(25);
        return user;
    }

    @PutMapping("restful04/{id}")
    public User restful04(@PathVariable int id) {
        System.out.println("put");
        User user = new User();
        user.setUsername("zhangsan");
        user.setAge(25);
        return user;
    }

}
